package com.vasthu.core.service;

import com.vasthu.api.dao.BillDAO;
import com.vasthu.api.domain.Bill;
import java.util.*;

import com.vasthu.gui.VstTableItemSelectable;
import com.vasthu.api.service.BillManagerService;
import com.vasthu.api.service.StockManagerService;
import com.vasthu.api.service.CategoryManagerService;
import com.vasthu.api.service.ItemManagerService;
import com.vasthu.gui.VstTableItemSelectable;
import com.vasthu.gui.VstTableItem;
import com.vasthu.api.domain.Stock;
import com.vasthu.api.domain.Category;
import com.vasthu.api.domain.Item;
import com.vasthu.core.dao.BillDAOImpl;
import com.vasthu.core.dao.UserDAOImpl;
import com.vasthu.utility.VstDBManager;
import java.sql.Connection;
import org.apache.log4j.Logger;

public class BillManagerServiceImpl implements BillManagerService {

    static Logger logger = Logger.getLogger(UserDAOImpl.class);
    private StockManagerService stockManagerService = null;
    private ItemManagerService itemManagerService = null;
    private CategoryManagerService categoryManagerService = null;
    BillDAO billDAO = null;

    public BillManagerServiceImpl() {
        stockManagerService = new StockManagerServiceImpl();
        itemManagerService = new ItemManagerServiceImpl();
        categoryManagerService = new CategoryManagerServiceImpl();
        billDAO = new BillDAOImpl();
    }

    @Override
    public boolean insertBill(Connection con, Bill bill) {
        if (bill == null) {
            return false;
        }
        boolean ok = false;
        ok = billDAO.insertBill(bill);
        return ok;
    }

    public List<VstTableItemSelectable> getAllStockItemsForSelectionTable() {
        List<VstTableItemSelectable> itemList = new ArrayList<VstTableItemSelectable>();

        List<VstTableItemSelectable> stockList = stockManagerService.getAllStockedItemsForTable();

        for (VstTableItemSelectable stock : stockList) {
            if (stock instanceof Stock) {
                Stock stk = (Stock) stock;
                if (stk != null) {
                    VstTableItem tblItem = new VstTableItem();
                    tblItem.setStockId(stk.getStockId());
                    tblItem.setQuentity(stk.getQuantity());
                    tblItem.setUnitPrice(stk.getUnitPrice());

                    Item item = itemManagerService.getItemById(stk.getItemId());
                    if (item != null) {
                        tblItem.setItemCode(item.getItemCode());
                        tblItem.setItemName(item.getItemName());

                        Category cat = categoryManagerService.getCategoryById(item.getItemCategoryId());
                        if (cat != null) {
                            tblItem.setItemCategoryName(cat.getCategoryName());
                        }
                    }

                    itemList.add(tblItem);
                }
            }
        }
        return itemList;
    }
}